var buildDepartmentAddForm = function() {
	var departmentAddForm = formPanel();
	
	var Name  = textField({id:'name', fieldLabel:'Name', allowBlank:true});	
	var Email = textField({id:'email', fieldLabel:'Email', allowBlank:true});
	var Phone = textField({id:'phone', fieldLabel:'Phone', allowBlank:true});
	
	departmentAddForm.add(Name);
	departmentAddForm.add(Email);
	departmentAddForm.add(Phone);
	
	return departmentAddForm;
}

var buildProjectAddForm = function() {
	var projectAddForm = formPanel();
	
	var Name = textField({id:'name', fieldLabel:'Name', allowBlank:true});	
	var Email = textField({id:'email', fieldLabel:'Email', allowBlank:true});
	var Phone = textField({id:'phone', fieldLabel:'Phone', allowBlank:true});
	
	projectAddForm.add(Name);
	projectAddForm.add(Email);
	projectAddForm.add(Phone);
	
	return projectAddForm;
}

var buildEmployeeAddForm = function() {
	var employeeAddForm = formPanel();
	
	var Name = textField({id:'name', fieldLabel:'Name', allowBlank:true});
	var BirthDate = dateField({id:'birthDate', fieldLabel:'Birth date', allowBlank:true});	
	var Email = textField({id:'email', fieldLabel:'Email', allowBlank:true});		
	var Phone = textField({id:'phone', fieldLabel:'Phone', allowBlank:true});	
	var HiredDate = dateField({id:'hiredDate', fieldLabel:'Hired date', allowBlank:true});	
	var Position = positionComboBox();
	
	employeeAddForm.add(Name);
	employeeAddForm.add(BirthDate);
	employeeAddForm.add(Email);
	employeeAddForm.add(Phone);
	employeeAddForm.add(HiredDate);
	employeeAddForm.add(Position);
	
	return employeeAddForm;
}

var createAddForm = function(selNode, treePanelObj) {	
	var selNodePath = selNode.getPath();
	
	if (selNode.id == '_treeRoot_') {
		var form = buildDepartmentAddForm();
		var wTitle = "Add new Department";
	}
	else if (selNode.id.match('_DEP_')) { 
		var form = buildProjectAddForm();
		var wTitle = "Add new Project";
	}
	else if (selNode.id.match('_PRJ_')) {
		var form = buildEmployeeAddForm();
		var wTitle = "Add new Employee";
	}

    var window = new Ext.Window({
        title: wTitle,
        width: 400,
        layout: 'fit',
        plain:true,
        bodyStyle:'padding:5px;',
        buttonAlign:'center',
        items: form,

        buttons: [{
        	text: 'Add',
        	handler: function() {
        		if(form.hasChanged) {
	        		form.getForm().submit({
	        			url : 'dispatcher/addElement',
	        			params : {
	        				parentNode : selNode.id,
	        				parentNodeName : selNode.text
	        			},
	        			waitTitle: 'Please wait...',
	        			waitMsg: 'Wait...',
	        			success: function(form, action) {
	        				var result = action.result;
	        				if(result.success === 'true') {
	        					window.close();	                	
	            				root = treePanelObj.getRootNode();
	            				root.reload();
	            				treePanelObj.expandPath(selNodePath);
	        				} else {
	        					processErrors(result);
	        				}
	        			},
	        			failure: function(form, action) {
	        				switch (action.failureType) {
	        				case Ext.form.Action.CLIENT_INVALID:
	        	                Ext.Msg.alert('ERROR', 'Form fields may not be submitted with invalid values');
	        	                break;
	        	            case Ext.form.Action.CONNECT_FAILURE:
	        	                Ext.Msg.alert('ERROR', 'Ajax communication failed');
	        	                break;
	        	            case Ext.form.Action.SERVER_INVALID:
	        	            	Ext.Msg.alert('ERROR', 'An error occured with the server.\n' + action.result);
	        				}
	        			}
	        		});
        		}
        	}
        },
        {
        	text: 'Clear',
        	handler : function(btn, evt) {
        		form.getForm().reset();
        	}
        }]
    });

    window.show();
};